package com.jrx.anytxn.param.controller.aviator;

import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnRuntimeException;
import com.jrx.anytxn.param.dto.aviator.AviatorDicInfoRes;
import com.jrx.anytxn.param.dto.aviator.TransactionDicInfoRes;
import com.jrx.anytxn.param.service.aviator.IDicInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 规则条件信息的接口
 *
 * @author Qingyao Xing
 * @date 2019/7/12 17:53
 */
@Api(tags = "条件信息的的管理")
@RestController
@RequestMapping("/param/rules/dicts")
public class DicInfoController {

    @Autowired
    private IDicInfoService dicInfoService;

    @Autowired
    private InfoService infoService;

    private static Logger logger = LoggerFactory.getLogger(DicInfoController.class);


    /***根据参数查询条件信息
     *  * @param ruleType
     * @param organizationNumber
     * @return jrx.anytxn.biz.common.web.HttpApiResponse
     * @throw
     * @author Qingyao Xing
     * @date 2019/7/12 17:53
     */
    @GetMapping(value = "/rulesList")
    public TxnRespResult getDicts(@RequestParam("ruleType") String ruleType, @RequestParam("organizationNumber") String organizationNumber) {
        TxnRespResult<ArrayList<AviatorDicInfoRes>> result = new TxnRespResult<>();
        try {
            String tenantId= String.valueOf(infoService.getUser().getProjectId());
            List<AviatorDicInfoRes> list = dicInfoService.getAllDicByParams(ruleType,tenantId);

            result = result.getSuccess((ArrayList<AviatorDicInfoRes>) list);
        } catch (TxnRuntimeException e) {
            result.setCode(e.getErrCode());
            result.setMessage(e.getErrMsg());
            logger.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 交易识别规则获取规则下拉结果
     */
    @ApiOperation(value = "交易识别规则获取规则结果")
    @GetMapping(value = "transactions")
    public TxnRespResult getTransactionIdentificationParam() {
        TxnRespResult<ArrayList<TransactionDicInfoRes>> result = new TxnRespResult<>();
        try {
            List<TransactionDicInfoRes> list = dicInfoService.getTransactionIdentificationParam();
            result = result.getSuccess((ArrayList<TransactionDicInfoRes>) list);
        } catch (TxnRuntimeException e) {
            result.setCode(e.getErrCode());
            result.setMessage(e.getErrMsg());
            logger.error(e.getMessage(), e);
        }
        return result;
    }
}
